package com.itboye.mengwu.app.ui.aiwujia;

import java.util.Observable;
import java.util.Observer;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.presenter.NewMaMaZengPresenter;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class ActivityMaMaZengWuLiuXinXiTianXie extends BaseActivity implements Observer {

	TextView txt_title, wuliugongsi,okTv;
	ImageView back;
	LinearLayout wuliuxuanzeLine;

	TextView shunfengTv,shentongTv,yundaTv,baishiTv,zhongtongTv,yuantongTv;

	boolean flag = false;
	PopupWindow popupWindow;
	
	String linkMan,address,phoneNum,reason,uid,pid,express_code;
	
	TextView linkManTv,addressTv,phoneNumTv,reasonTv;
	
	NewMaMaZengPresenter newMaMaZengPresenter;
	EditText wuliudanhao;

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = super.handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == NewMaMaZengPresenter.FahuoSuccess) {
				String str= (String) entity.getData();
				ByAlert.alert(str);
				finish();
				
			}else if(entity.getEventType() == NewMaMaZengPresenter.FahuoFail){
				ByAlert.alert("提交失败！");

			}
		}

	}

	@Override
	public int initView() {
		return R.layout.activity_mamazeng_fahuowuliuxinxi;
	}

	@Override
	public void onMyCreate() {
		newMaMaZengPresenter =new NewMaMaZengPresenter(this);
		txt_title.setText("物流信息填写");
		
		Intent intent= getIntent();
		linkMan = intent.getStringExtra("linkMan");
		address =intent.getStringExtra("address");
		phoneNum =intent.getStringExtra("phoneNum");
		reason =intent.getStringExtra("reason");
		uid =intent.getStringExtra("uid");
		pid =intent.getStringExtra("pid");
		
		
		linkManTv.setText(linkMan);
		addressTv.setText(address);
		phoneNumTv.setText(phoneNum);
		reasonTv.setText(reason);	
		
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();
			}
		});
		
		wuliuxuanzeLine.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showPopupWindow(v);

			}

		});
		
		okTv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(TextUtils.isEmpty(express_code)){
					ByAlert.alert("请选择快递！");
				}else {
					if (TextUtils.isEmpty(wuliudanhao.getText()+"")) {
						ByAlert.alert("请输入快递单号！");
					}else {
						newMaMaZengPresenter.fahuo(uid, pid, wuliudanhao.getText()+"", express_code);
					}
					
				}
			
			}
		});
	}

	private void showPopupWindow(View v) {
		if (!flag) {
			View contentView = LayoutInflater.from(this).inflate(R.layout.item_mamazeng_kuaidixuanze, null);

			shunfengTv = (TextView) contentView.findViewById(R.id.shunfengTv);
			shentongTv = (TextView) contentView.findViewById(R.id.shentongTv);
			yundaTv = (TextView) contentView.findViewById(R.id.yundaTv);
			baishiTv = (TextView) contentView.findViewById(R.id.baishiTv);
			zhongtongTv = (TextView) contentView.findViewById(R.id.zhongtongTv);
			yuantongTv = (TextView) contentView.findViewById(R.id.yuantongTv);

			shunfengTv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					wuliugongsi.setText(shunfengTv.getText());
					express_code="6005";
					popupWindow.dismiss();
				}
			});
			shentongTv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					wuliugongsi.setText(shentongTv.getText());
					express_code="6006";
					popupWindow.dismiss();
				}
			});
			yundaTv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					wuliugongsi.setText(yundaTv.getText());
					express_code="6060";
					popupWindow.dismiss();
				}
			});
			baishiTv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					wuliugongsi.setText(baishiTv.getText());
					express_code="6061";
					popupWindow.dismiss();
				}
			});
			zhongtongTv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					wuliugongsi.setText(zhongtongTv.getText());
					express_code="6062";
					popupWindow.dismiss();
				}
			});
			yuantongTv.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					wuliugongsi.setText(yuantongTv.getText());
					express_code="6063";
					popupWindow.dismiss();
				}
			});
			

			

			popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			popupWindow.setBackgroundDrawable(new BitmapDrawable());
			popupWindow.setTouchable(true);
			popupWindow.setOutsideTouchable(true);
			popupWindow.showAsDropDown(v);
		} else {
			popupWindow.dismiss();
		}

	}
}
